# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer:
pkgname=mingw-w64-binutils
pkgver=2.38
pkgrel=0
pkgdesc="Cross binutils for the MinGW-w64 cross-compiler"
url="https://www.gnu.org/software/binutils"
arch="x86_64 x86"
license="GPL-3.0-or-later"
makedepends="zlib-dev perl texinfo"
subpackages="$pkgname-dbg $pkgname-doc"
source="$pkgname-$pkgver.tar.xz::https://ftp.gnu.org/gnu/binutils/binutils-$pkgver.tar.xz"
builddir="$srcdir/binutils-$pkgver"

case "$CARCH" in
	x86_64) _target="x86_64-w64-mingw32" ;;
	x86) _target="i686-w64-mingw32" ;;
esac

# Directory for out-of-tree builds
_binutilsdir="$srcdir/binutils-$_target"

# secfixes:
#   2.36-r0:
#     - CVE-2020-35448

prepare() {
	default_prepare

	# Do not install libiberty
	sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
	# hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

	mkdir -p "$_binutilsdir"
}

build() {
	cd "$_binutilsdir"
	"$builddir"/configure \
		--prefix=/usr \
		--target=$_target \
		--infodir=/usr/share/info/$_target \
		--enable-lto --enable-plugins \
		--enable-deterministic-archives \
		--disable-multilib --disable-nls \
		--disable-werror
	make
}

package() {
	cd "$_binutilsdir"
	make DESTDIR="$pkgdir" install

	# delete bfd-plugins directory, so it does not conflict with host toolchain
	rm -rf "$pkgdir"/usr/lib/bfd-plugins
}

sha512sums="
8bf0b0d193c9c010e0518ee2b2e5a830898af206510992483b427477ed178396cd210235e85fd7bd99a96fc6d5eedbeccbd48317a10f752b7336ada8b2bb826d  mingw-w64-binutils-2.38.tar.xz
"
